package au.edu.swin.swinvehicle.vehiclelicencetype;

/**
 * VehicleLicenceTypeManagerBean
 * @version 1.0
 * @author Viet Bui
 * @lastmodify 2009-05-12
 */
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import com.sun.org.apache.commons.logging.Log;
import com.sun.org.apache.commons.logging.LogFactory;

@Stateless
public class VehicleLicenceTypeManagerBean implements VehicleLicenceTypeManagerRemote {
private static Log log = LogFactory.getLog(VehicleLicenceTypeManagerBean.class);
    @PersistenceContext
    private EntityManager em;

     public void addVehicleLicenceType(VehicleLicenceTypeDetailedTO to) throws VehicleLicenceTypeManagerException {

        try {

            VehicleLicenceTypeEntity vlt = new VehicleLicenceTypeEntity();
            vlt.setData(to);
            em.persist(vlt);

        } catch (Exception e) {
            throw new VehicleLicenceTypeManagerException("Cannot add Vehicle Licence Type", e);
        }
    }

    public void editVehicleLicenceType(VehicleLicenceTypeDetailedTO to) throws VehicleLicenceTypeManagerException {
        try {

            VehicleLicenceTypeEntity vlt = em.find(VehicleLicenceTypeEntity.class, to.getId());
            vlt.setData(to);
            em.persist(vlt);

        } catch (Exception e) {
            throw new VehicleLicenceTypeManagerException("Cannot edit Vehicle Licence Type", e);
        }

    }

    public void deleteVehicleLicenceType(Long id) throws VehicleLicenceTypeManagerException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    public List<VehicleLicenceTypeTO> getAllVehicleLicenceTypes() throws VehicleLicenceTypeManagerException {
        try {
            List<VehicleLicenceTypeTO> results = new ArrayList<VehicleLicenceTypeTO>();
            Query allVehicleLicenceTypesQuery = em.createNamedQuery("findAllVehicleLicenceTypes");
            List dbResults = allVehicleLicenceTypesQuery.getResultList();
            for (int i = 0; i < dbResults.size(); i++) {
                VehicleLicenceTypeEntity vlt = (VehicleLicenceTypeEntity) dbResults.get(i);
                results.add(vlt.getData());
            }
            return results;
        } catch (Exception e) {
            throw new VehicleLicenceTypeManagerException("Cannot get all vehicle licence types", e);
        }
    }

        public List<VehicleLicenceTypeDetailedTO> getAllDetailedVehicleLicenceTypes() throws VehicleLicenceTypeManagerException {
        try {
            List<VehicleLicenceTypeDetailedTO> results = new ArrayList<VehicleLicenceTypeDetailedTO>();
            Query allVehicleLicenceTypesQuery = em.createNamedQuery("findAllVehicleLicenceTypes");
            List dbResults = allVehicleLicenceTypesQuery.getResultList();
            for (int i = 0; i < dbResults.size(); i++) {
                VehicleLicenceTypeEntity vlt = (VehicleLicenceTypeEntity) dbResults.get(i);
                results.add(vlt.getDetailedData());
            }
            return results;
        } catch (Exception e) {
            throw new VehicleLicenceTypeManagerException("Cannot get all VehicleLicenceTypeDetailedTOs", e);
        }
    }

    public VehicleLicenceTypeDetailedTO getVehicleLicenceType(Long id) throws VehicleLicenceTypeManagerException {
        try {
            VehicleLicenceTypeEntity vlt = em.find(VehicleLicenceTypeEntity.class, id);
            return vlt.getDetailedData();
        } catch (Exception e) {
            throw new VehicleLicenceTypeManagerException("Cannot get vehicle licence type by id " + id.longValue(), e);
        }
    }
}